package com.zte.shopping.service.impl;

import com.zte.shopping.action.backend.BackendDeptManagerController;
import com.zte.shopping.constant.DictConstant;
import com.zte.shopping.constant.StatusConstant;
import com.zte.shopping.entity.Dept;
import com.zte.shopping.entity.Sequence;
import com.zte.shopping.entity.Staff;
import com.zte.shopping.exception.DeptExistException;
import com.zte.shopping.exception.LoginDisabledException;
import com.zte.shopping.exception.RequestParameterException;
import com.zte.shopping.mapper.dept.DeptMapper;
import com.zte.shopping.mapper.sequence.SequenceMapper;
import com.zte.shopping.service.DeptService;
import com.zte.shopping.util.Constants;
import com.zte.shopping.util.ParameterUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: ssm
 * @description: 部门管理deptService实现类
 * @author: zuti
 * @create: 2020-06-28 17:50
 **/
@Service
public class DeptServiceImpl implements DeptService {

    private  static  final Logger logger= Logger.getLogger(DeptServiceImpl.class);

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    SequenceMapper sequenceMapper;

    /**
     * @param pageIndex
     * @return java.util.List<com.zte.shopping.entity.Dept>
     * @Author zuti
     * @Description //TODO 查询部门列表
     * @Date 17:54 2020/6/28
     * @Param [pageIndex]
     */
    @Override
    public List<Dept> deptList(Integer pageIndex) {
        int pageSize = Constants.pageSize;
        int from = (pageIndex-1)*pageSize;
        logger.debug("enter DeptServiceImpl ------> deptList param: pageIndex="+pageIndex+" pageSize="+pageSize+" from="+from);
        return  deptMapper.selectAll(from,pageSize);
    }

    /**
     * @return int
     * @Author zuti
     * @Description //TODO 查询部门数量
     * @Date 17:54 2020/6/28
     * @Param []
     **/
    @Override
    public int deptListCount() {
        logger.debug("enter DeptServiceImpl ------> deptListCount()");
        return deptMapper.deptListCount();
    }

    /**
     * @Author zuti
     * @Description //TODO 添加部门
     * @Date 15:22 2020/6/30
     * @Param [deptName, remark, session]
     * @return void
     **/
    @Override
    public void addFatherDept(String deptName, String remark, HttpSession session) throws DeptExistException, LoginDisabledException {
        Dept dept = deptMapper.selectByName(deptName);
        if (dept != null)
        {
            throw new DeptExistException("该部门已经存在!");
        }

        Staff staff = (Staff) session.getAttribute("staff");
        if (staff == null)
        {
            throw new LoginDisabledException("登录失效, 请重新登录");
        }

        Dept d = new Dept();
        d.setCreateDate(new Date());
        d.setDeptName(deptName);
        // 部门状态为有效,值为1
        d.setIsValid(StatusConstant.DEPT_STATUS_ENABLE);
        d.setRemark(remark);
        d.setStaff(staff);

        // DictConstant.DEPT_NO_PREFIX: 部门编号前缀  BM
        Sequence sequence = sequenceMapper.selectByName(DictConstant.DEPT_NO_PREFIX);
        if (sequence == null)
        {
            Sequence sequ = new Sequence();

            sequ.setName(DictConstant.DEPT_NO_PREFIX); //  DictConstant.DEPT_NO_PREFIX: 部门编号前缀  BM
            sequ.setValue(DictConstant.DEPT_NO_SEQUENCE_MIN);   // DictConstant.DEPT_NO_SEQUENCE_MIN: 部门编号的序列号最小值为000001

            // 添加序列
            sequenceMapper.insertSequence(sequ);

            d.setDeptNo(DictConstant.DEPT_NO_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + sequ.getValue());
        }else
        {
            String value = sequence.getValue();

            // 若当前的序列号达到了最大值, 则初始化设置位最小值, 从头开始
            if (DictConstant.DEPT_NO_SEQUENCE_MAX.equals(value))
            {
                value = DictConstant.DEPT_NO_SEQUENCE_MIN;
            }else
            {
                value = String.format("%06d", Integer.parseInt(value) + 1);
            }

            sequenceMapper.updateSquenceValue(DictConstant.DEPT_NO_PREFIX, value);

            d.setDeptNo(DictConstant.DEPT_NO_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + sequence.getValue());
        }

        // 添加父部门
        deptMapper.insertFatherDept(d);
    }
    /**
     * @Author zuti
     * @Description //TODO 添加子部门
     * @Date 15:23 2020/6/30
     * @Param [fatherDeptId, deptName, remark, session]
     * @return void
     **/
    @Override
    public void addSonDept(String fatherDeptId, String deptName, String remark, HttpSession session) throws DeptExistException, LoginDisabledException {
        Dept dept = deptMapper.selectByName(deptName);

        if (dept != null)
        {
            throw new DeptExistException("该部门已经存在!");
        }

        Staff staff = (Staff) session.getAttribute("staff");
        if (staff == null)
        {
            throw new LoginDisabledException("登录失效, 请重新登录");
        }

        Dept d = new Dept();
        d.setCreateDate(new Date());
        d.setDeptName(deptName);
        // 部门状态为有效,值为1
        d.setIsValid(StatusConstant.DEPT_STATUS_ENABLE);
        d.setRemark(remark);
        d.setStaff(staff);


        // 和添加父部门时不同  start
        Dept fatherDept = new Dept();
        fatherDept.setDeptId(Integer.parseInt(fatherDeptId));
        d.setFatherDept(fatherDept);
        // 和添加父部门时不同  end

        // DictConstant.DEPT_NO_PREFIX: 部门编号前缀  BM
        Sequence sequence = sequenceMapper.selectByName(DictConstant.DEPT_NO_PREFIX);
        if (sequence == null)
        {
            Sequence sequ = new Sequence();

            sequ.setName(DictConstant.DEPT_NO_PREFIX); //  DictConstant.DEPT_NO_PREFIX: 部门编号前缀  BM
            sequ.setValue(DictConstant.DEPT_NO_SEQUENCE_MIN);   // DictConstant.DEPT_NO_SEQUENCE_MIN: 部门编号的序列号最小值为000001

            // 添加序列
            sequenceMapper.insertSequence(sequ);

            d.setDeptNo(DictConstant.DEPT_NO_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + sequ.getValue());
        }else
        {
            String value = sequence.getValue();

            // 若当前的序列号达到了最大值, 则初始化设置位最小值, 从头开始
            if (DictConstant.DEPT_NO_SEQUENCE_MAX.equals(value))
            {
                value = DictConstant.DEPT_NO_SEQUENCE_MIN;
            }else
            {
                value = String.format("%06d", Integer.parseInt(value) + 1);
            }

            sequenceMapper.updateSquenceValue(DictConstant.DEPT_NO_PREFIX, value);

            d.setDeptNo(DictConstant.DEPT_NO_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + sequence.getValue());
        }

        // 添加子部门操作
        deptMapper.insertSonDept(d);
    }

    /**
     * @Author zuti
     * @Description //TODO 修改部门
     * @Date 15:23 2020/6/30
     * @Param [deptId, deptName, remark]
     * @return void
     **/
    @Override
    public void modifyDept(String deptId, String deptName, String remark) throws DeptExistException {
        Dept dept = deptMapper.selectByIdAndName(Integer.parseInt(deptId), deptName);

        if (dept != null)
        {
            throw new DeptExistException("该部门名称已经存在!");
        }

        // 修改部门信息
        deptMapper.updateDept(Integer.parseInt(deptId), deptName, remark);
    }

    /**
     * @Author zuti
     * @Description //TODO 修改部门状态
     * @Date 15:23 2020/6/30
     * @Param [deptId, isValid]
     * @return void
     **/
    @Override
    public void modifyStatus(String deptId, String isValid) throws RequestParameterException {
        if (ParameterUtil.isnull(deptId))
        {
            throw new RequestParameterException("部门ID不能为空");
        }

        if (ParameterUtil.isnull(isValid))
        {
            throw new RequestParameterException("部门状态不能为空");
        }

        Integer status = Integer.parseInt(isValid);
        Integer DeptId =Integer.valueOf(deptId);

        if (status == StatusConstant.DEPT_STATUS_ENABLE)  // 启用  ---> 禁用
        {
            status = StatusConstant.DEPT_STATUS_DISABLE;
        }else                                             // 禁用  ---> 启用
        {
            status = StatusConstant.DEPT_STATUS_ENABLE;
        }

        // 子部门的 启用/禁用
        deptMapper.updateStatus(DeptId, status);
    }


    /**
     * @Author zuti
     * @Description //TODO 查找部门通过Id
     * @Date 15:24 2020/6/30
     * @Param [id]
     * @return com.zte.shopping.entity.Dept
     **/
    @Override
    public Dept findById(String id) throws RequestParameterException {
        if (ParameterUtil.isnull(id))
        {
            throw new RequestParameterException("部门id不能为空");
        }

        Dept dept = deptMapper.selectById(Integer.parseInt(id));
        return dept;
    }
}
